import http from '../libs/request'

export interface UserProps {
    email: string;
    captcha: string;
    username: string;
    password: string;
    confirm_password: string;
}

export const register = (data: UserProps) => {
    return http({
      url: `/auth/register`,
      method: 'post',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data
    })
}

export const login = (data: {email: string; password: string}) => {
  return http({
    url: `/auth/login`,
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data
  })
}

export const send_email = (email: string) => {
    return http({
      url: `/auth/captcha/email?email=${email}`,
      method: 'get',
    })
}

export const publish = (data: {title: string; content: string}) => {
  return http({
    url: `/qa/public`,
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data
  })
}

export interface QuestionProps {
  title: string;
  content: string;
  id: number;
  author?: string;
  create_time?: string;
}

export const get_questions = () => {
  return http<API.Result<QuestionProps[]>>({
    url: `/qa/questions`,
    method: 'get',
  })
}

export const get_question = (id: string) => {
  return http<API.Result<QuestionProps>>({
    url: `/qa/question/${id}`,
    method: 'get',
  })
}

export const search_qa = (q: string) => {
  return http<API.Result<QuestionProps[]>>({
    url: `/qa/search?q=${q}`,
    method: 'post',
  })
}

export const public_answer = (data: {content: string; question_id: string}) => {
  return http({
    url: `/answer/public`,
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data
  })
}